<?php
class fwDbObject
{
	public $id = null;
	
	protected $_table_name = '';
	protected $_table_prefix = '';
	protected $_table_fields = array();

	protected $_loaded = false;

	public function __construct($id=null)
	{
		$this->id = $id;
	}
	
	public function load($id=null)
	{
		global $conf;
		if($this->_loaded && $id === null)
		{
			return;
		}
		if($id !== null)
		{
			$this->id = $id;
		}
		if($this->id === null)
		{
			return;
		}
		$sql = 'SELECT
					*
				FROM
					'.$conf['bdd']['prefix'].''.$this->_table_name.'
				WHERE
					'.$this->_table_prefix.'_i_id='.$this->id;
		$row = fwDb::getRow($sql);
		$this->loadFromRow($row);
	}
	
	public function loadFromRow($row)
	{
		if($row === false)
		{
			$this->id = null;
		}
		else 
		{
			$this->id = $row[$this->_table_prefix.'_i_id'];
			foreach ($this->_table_fields as $db_name => $class_name)
			{
				$this->$class_name = $row[$db_name];
			}
			$this->_loaded = true;
		}
	}
	
	public function delete($id=null)
	{
		global $conf;
		if($id!==null)
		{
			$this->id = $id;
		}
		$sql = 'UPDATE
					'.$conf['bdd']['prefix'].''.$this->_table_name.'
				SET
					'.$this->_table_prefix.'_d_delted=NOW()';
		fwDb::setData($sql);
	}
	
	public function save()
	{
		global $conf;
		$fields = '';
		foreach ($this->_table_fields as $db_name => $class_name)
		{
			$fields .= ', ' . $db_name . "='".addslashes($this->$class_name)."'
						";
		}
		if($this->id===null)
		{
			$sql = 'INSERT INTO
						'.$conf['bdd']['prefix'].''.$this->_table_name.'
					SET
						'.$this->_table_prefix.'_d_created=NOW()
						, '.$this->_table_prefix.'_d_updated=NOW()
						, '.$this->_table_prefix.'_d_deleted=NULL
						'.$fields;
			fwDb::setData($sql);
			$this->id = fwDb::insertedId();
		}
		else 
		{
			$sql = 'UPDATE
						'.$conf['bdd']['prefix'].''.$this->_table_name.'
					SET
						'.$this->_table_prefix.'_d_updated=NOW()
						, '.$this->_table_prefix.'_d_deleted=NULL
						'.$fields.'
					WHERE
						'.$this->_table_prefix.'_i_id='.$this->id;
			fwDb::setData($sql);
		}
	}
	
	public static function getListFromIds($object_class, $ids)
	{
		if(!is_array($ids))
		{
			if($ids!='')
			{
				$ids = array($ids);
			}
			else 
			{
				$ids = array();
			}
		}
		
		$int_ids = array();
		foreach ($ids as $id)
		{
			if(is_numeric($id))
			{
				$int_ids[] = $id;
			}
		}
		if(count($int_ids)==0)
		{
			return array();
		}

		$object = new $object_class();
		$where = $object->_table_prefix.'_i_id IN ('.implode(',', $int_ids).')';
		return fwDbObject::getList($object_class, $where);
	}
	
	public static function getListFromFields($object_class, $field, $ids)
	{
		if(!is_array($ids))
		{
			if($ids!='')
			{
				$ids = array($ids);
			}
			else 
			{
				$ids = array();
			}
		}
		
		if(count($ids)==0)
		{
			return array();
		}

		$object = new $object_class();
		$where = $object->_table_prefix.'_'.$field.' IN (\''.implode('\',\'', $ids).'\')';
		return fwDbObject::getList($object_class, $where);
	}
	
	public static function getList($object_class, $where='')
	{
		global $conf;
		$object = new $object_class();

		$sql = 'SELECT
					*
				FROM
					'.$conf['bdd']['prefix'].''.$object->_table_name.'
				WHERE
					'.$object->_table_prefix.'_d_deleted IS NULL
					';
		if($where!='')
		{
			$sql.='AND '.$where;
		}
		
		$datas = fwDb::getData($sql);
		$objects = array();
		if($datas!==false)
		{
			foreach ($datas as $row)
			{
				$o = new $object_class();
				$o->loadFromRow($row);
				$objects[] = $o;
			}
		}
		return $objects;
	}
}
?>